package com.ruoyi.system.api.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.*;
import com.ruoyi.system.api.mapper.*;
import com.ruoyi.system.api.service.CcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.api.service.IStReportPortService;
import com.ruoyi.common.core.text.Convert;

/**
 * 报告与备件的关系 （包括服务报告和电话解决报告）Service业务层处理
 *
 * @author ruoyi
 * @date 2020-03-03
 */
@Service
public class StReportPortServiceImpl implements IStReportPortService {


    @Autowired
    private AnnexMapper annexMapper;

    @Autowired
    private ReportInfoMapper reportInfoMapper;

    @Autowired
    private StPhoneReportMapper phoneReportMapper;

    @Autowired
    private StServiceReportMapper serviceReportMapper;

    @Autowired
    private StReportPortMapper stReportPortMapper;

    @Autowired
    private ReportInfoMapper stReportInfoMapper;


    @Autowired
    private CcService ccService;

    /**
     * 查询报告与备件的关系 （包括服务报告和电话解决报告）
     *
     * @param id 报告与备件的关系 （包括服务报告和电话解决报告）ID
     * @return 报告与备件的关系 （包括服务报告和电话解决报告）
     */
    @Override
    public StReportPort selectStReportPortById(Long id) {
        return stReportPortMapper.selectStReportPortById(id);
    }

    /**
     * 查询报告与备件的关系 （包括服务报告和电话解决报告）列表
     *
     * @param stReportPort 报告与备件的关系 （包括服务报告和电话解决报告）
     * @return 报告与备件的关系 （包括服务报告和电话解决报告）
     */
    @Override
    public List<StReportPort> selectStReportPortList(StReportPort stReportPort) {
        return stReportPortMapper.selectStReportPortList(stReportPort);
    }

    /**
     * 新增报告与备件的关系 （包括服务报告和电话解决报告）
     *
     * @param stReportPort 报告与备件的关系 （包括服务报告和电话解决报告）
     * @return 结果
     */
    @Override
    public int insertStReportPort(StReportPort stReportPort) {
        stReportPort.setCreateTime(DateUtils.getNowDate());
        return stReportPortMapper.insertStReportPort(stReportPort);
    }

    /**
     * 修改报告与备件的关系 （包括服务报告和电话解决报告）
     *
     * @param stReportPort 报告与备件的关系 （包括服务报告和电话解决报告）
     * @return 结果
     */
    @Override
    public int updateStReportPort(StReportPort stReportPort) {
        stReportPort.setUpdateTime(DateUtils.getNowDate());
        return stReportPortMapper.updateStReportPort(stReportPort);
    }

    /**
     * 删除报告与备件的关系 （包括服务报告和电话解决报告）对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStReportPortByIds(String ids) {
        return stReportPortMapper.deleteStReportPortByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除报告与备件的关系 （包括服务报告和电话解决报告）信息
     *
     * @param id 报告与备件的关系 （包括服务报告和电话解决报告）ID
     * @return 结果
     */
    @Override
    public int deleteStReportPortById(Long id) {
        return stReportPortMapper.deleteStReportPortById(id);
    }


    /**
     * 删除安装质量报告
     *
     * @param id 报告与备件的关系 （包括服务报告和电话解决报告）ID
     * @return 结果
     */
    @Override
    public int removeReportPortByInstallation(Long id) {
        StReportPort stReportPort = new StReportPort();
        stReportPort.setId(id);
        stReportPort.setDelFlag("2");
        // stReportPort.setPreDelFlag("1");
        return stReportPortMapper.updateStReportPort(stReportPort);
    }


    /**
     * 添加 1、服务报告 2、电话解决报告 3、Error Report 4、Hot Account Report等报告备件信息
     *
     * @param reportPort
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020-03-03 14:00
     */
    @Override
    public ResponseWithData<?> addReportPort(StReportPort reportPort) {
        // 参数校验 start
        if (reportPort.getEnginnerId() == null) {
            return new ResponseWithData<>().error("enginnerId cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getServiceNo())) {
            return new ResponseWithData<>().error("serviceNo cannot be empty！");
        }
        if (null == reportPort.getQtyCount()) {
            return new ResponseWithData<>().error("qtyCount cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getPartNumber())) {
            return new ResponseWithData<>().error("partNumber cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getHelpId())) {
            return new ResponseWithData<>().error("helpId cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getAnnexHelpId())) {
            return new ResponseWithData<>().error("annexHelpId cannot be empty！");
        }
        // 参数校验 end
        // 数据入库 strat
        reportPort.setCreateTime(new Date());
        reportPort.setDelFlag("0");
        reportPort.setSyncFlag("0");
        stReportPortMapper.insertStReportPort(reportPort);

        if (StringUtils.isNotEmpty(reportPort.getHelpId())) {
            Annex annex = new Annex();
            annex.setUpdateTime(new Date());
            annex.setRelationId(reportPort.getId().toString());
            annex.setTblName("st_report_port");
            annex.setHelpId(reportPort.getHelpId());
            annex.setServiceNo(reportPort.getServiceNo());
            annexMapper.updateAnnexByHelpIdAndServiceNo(annex);
        }

        // 数据入库 end

        //更新CC备件状态
        if(!StringUtils.isEmpty(reportPort.getPid())){
            ccService.deleteCCParts(reportPort.getPid());
        }
        return new ResponseWithData<>().success(reportPort).code(200);
    }


    /**
     * 移除1、服务报告 2、电话解决报告 3、Error Report 4、Hot Account Report等报告备件信息
     *
     * @param enginnerId   工程师ID
     * @param reportPortId 备件表主键ID
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020-03-03 15:02
     */
    @Override
    public ResponseWithData<?> removeReportPort(Long enginnerId, Long reportPortId, String pid) {
        StReportPort reportPort = new StReportPort();
        reportPort.setId(reportPortId);
        reportPort.setUpdateBy(enginnerId.toString());
        reportPort.setUpdateTime(new Date());
        annexMapper.deleteAnnexByRelationId(reportPortId.toString());
        stReportPortMapper.updateStReportPortById(reportPort);
        if(!StringUtils.isEmpty(pid)){
            ccService.addCCParts(pid);
        }

        return new ResponseWithData<>().success("移除备件信息成功").code(200);
    }

    /**
     * 查询1、服务报告 2、电话解决报告 3、Error Report 4、Hot Account Report等报告备件信息
     *
     * @param serviceNo serviceNo
     * @param helpId    报告辅助id
     * @param type      查询类型 1、服务报告 2、电话解决报告 3、Error Report 4、Hot Account Report
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020-03-03 15:56
     */
    @Override
    public ResponseWithData<?> selectReportPorts(String serviceNo, String helpId, String type) {
        List<StReportPort> datas = null;
        if ("3".equals(type) || "4".equals(type)) {
//            // 如果有报告【服务报告或电话解决报告】，就自动带出最后一次报告里的备件(这个报告已完成未完成都可以）
//            ReportInfo reportInfo = reportInfoMapper.selectStReportPortListByLastPhoneReportOrServiceReport(serviceNo);
//            if (null != reportInfo) {
//                if (1 == reportInfo.getRelServiceNo()) {
//                    // 服务报告
//                    StServiceReport stServiceReport = serviceReportMapper.selectStServiceReportById(reportInfo.getBatchType());
//                    if (null != stServiceReport) {
//                        datas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(stServiceReport.getServiceNo(), stServiceReport.getHelpId(), "1");
//                    }
//                } else if (6 == reportInfo.getRelServiceNo()) {
//                    // 电话解决报告
//                    StPhoneReport stPhoneReport = phoneReportMapper.selectStPhoneReportById(reportInfo.getBatchType());
//                    if (null != stPhoneReport) {
//                        datas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(stPhoneReport.getServiceNo(), stPhoneReport.getHelpId(), "2");
//                    }
//                }
//            }
            int i = stReportPortMapper.getServiceAndPhoneReportCount(serviceNo);
            if (i != 0) {
                datas = stReportPortMapper.getSubmitServiceAndPhoneReportPorts(serviceNo);
            }
        }

        // 查询当前类型的备件信息
        List<StReportPort> currentTypePortDatas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(serviceNo, helpId, type);
        if (null == datas) {
            datas = new ArrayList<>();
        }
        if (null != currentTypePortDatas) {

            datas.addAll(currentTypePortDatas);
        }
        datas = removeDupliById(datas);
        for (StReportPort stReportPort : datas) {
            List<Annex> annexList = annexMapper.selectAnnexListByHelpIdAndServiceNo(serviceNo, stReportPort.getAnnexHelpId());
            stReportPort.setAnnexList(annexList);
        }
        return new ResponseWithData<>().success(datas).code(200);
    }

    public ResponseWithData<?> selectServiceReportPorts(String serviceNo, String helpId, String type) {
        List<StReportPort> datas = null;
        if ("3".equals(type) || "4".equals(type)) {
//            // 如果有报告【服务报告或电话解决报告】，就自动带出最后一次报告里的备件(这个报告已完成未完成都可以）
//            ReportInfo reportInfo = reportInfoMapper.selectStReportPortListByLastPhoneReportOrServiceReport(serviceNo);
//            if (null != reportInfo) {
//                if (1 == reportInfo.getRelServiceNo()) {
//                    // 服务报告
//                    StServiceReport stServiceReport = serviceReportMapper.selectStServiceReportById(reportInfo.getBatchType());
//                    if (null != stServiceReport) {
//                        datas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(stServiceReport.getServiceNo(), stServiceReport.getHelpId(), "1");
//                    }
//                } else if (6 == reportInfo.getRelServiceNo()) {
//                    // 电话解决报告
//                    StPhoneReport stPhoneReport = phoneReportMapper.selectStPhoneReportById(reportInfo.getBatchType());
//                    if (null != stPhoneReport) {
//                        datas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(stPhoneReport.getServiceNo(), stPhoneReport.getHelpId(), "2");
//                    }
//                }
//            }
            int i = stReportPortMapper.getServiceAndPhoneReportCount(serviceNo);
            if (i != 0) {
                datas = stReportPortMapper.getSubmitServiceAndPhoneReportPorts(serviceNo);
            }
        }

        // 查询当前类型的备件信息
        List<StReportPort> currentTypePortDatas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(serviceNo, helpId, type);
        if (null == datas) {
            datas = new ArrayList<>();
        }
        if (null != currentTypePortDatas) {

            datas.addAll(currentTypePortDatas);
        }
        datas = removeDupliById(datas);
        for (StReportPort stReportPort : datas) {
            List<Annex> annexList = annexMapper.selectAnnexListByHelpIdAndServiceNo(serviceNo, stReportPort.getAnnexHelpId());
            stReportPort.setAnnexList(annexList);
        }
        return new ResponseWithData<>().success(datas).code(200);
    }

    public static List<StReportPort> removeDupliById(List<StReportPort> persons) {
        Set<StReportPort> personSet = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
        personSet.addAll(persons);
        return new ArrayList<>(personSet);
    }

    /**
     * 查看备件信息
     * 已完成报告【服务报告或电话解决报告】中的备件信息。
     *
     * @param serviceNo
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/9 2:53 下午
     */
    @Override
    public ResponseWithData<?> selctcInstallationQualityReportPort(String serviceNo, String helpId, String type) {
        List<StReportPort> datas = null;
        if ("3".equals(type) || "4".equals(type) || "5".equals(type)) {
//            // 如果有报告【服务报告或电话解决报告】，就自动带出最后一次报告里的备件(这个报告已完成未完成都可以）
//            ReportInfo reportInfo = reportInfoMapper.selectStReportPortListByLastPhoneReportOrServiceReport(serviceNo);
//            if (null != reportInfo) {
//                if (1 == reportInfo.getRelServiceNo()) {
//                    // 服务报告
//                    StServiceReport stServiceReport = serviceReportMapper.selectStServiceReportById(reportInfo.getBatchType());
//                    if (null != stServiceReport) {
//                        datas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(stServiceReport.getServiceNo(), stServiceReport.getHelpId(), "1");
//                    }
//                } else if (6 == reportInfo.getRelServiceNo()) {
//                    // 电话解决报告
//                    StPhoneReport stPhoneReport = phoneReportMapper.selectStPhoneReportById(reportInfo.getBatchType());
//                    if (null != stPhoneReport) {
//                        datas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(stPhoneReport.getServiceNo(), stPhoneReport.getHelpId(), "2");
//                    }
//                }
//            }
            int i = stReportPortMapper.getServiceAndPhoneReportCount(serviceNo);
            if (i != 0) {
                datas = stReportPortMapper.getSubmitServiceAndPhoneReportPorts(serviceNo);
            }
        }

        // 查询当前类型的备件信息
        List<StReportPort> currentTypePortDatas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(serviceNo, helpId, type);
        if (null == datas) {
            datas = new ArrayList<>();
        }
        if (null != currentTypePortDatas) {

            datas.addAll(currentTypePortDatas);
        }
        datas = removeDupliById(datas);
        return new ResponseWithData<>().success(datas).code(200);
    }


    /**
     * 查看安装质量报告备件
     * 已完成报告【服务报告或电话解决报告】中的备件信息。
     *
     * @param serviceNo
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/9 2:53 下午
     */
    @Override
    public ResponseWithData<?> selctcInstallationQualityReportPortByInstallation(String serviceNo, String helpId, String type, String reportId) {

        List<StReportPort> datas = null;
        List<StReportPort> stDatas = null;
        if ("3".equals(type) || "4".equals(type) || "5".equals(type)) {
            int i = stReportPortMapper.getServiceAndPhoneReportCount(serviceNo);
            if (i != 0) {
                datas = stReportPortMapper.getSubmitServiceAndPhoneReportPorts(serviceNo);
            }
        }


        // 查询当前类型的备件信息
        List<StReportPort> currentTypePortDatas = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(serviceNo, helpId, type);
        if (null == datas) {
            datas = new ArrayList<>();
        }
        if (null != currentTypePortDatas) {

            datas.addAll(currentTypePortDatas);
        }
        datas = removeDupliById(datas);
        // 查询
        StReportPort searchStReportPort = new StReportPort();
        searchStReportPort.setServiceNo(serviceNo);
        searchStReportPort.setPortType("5");
        searchStReportPort.setDelFlag("0");
        //  searchStReportPort.setSsflag("0");
        List<StReportPort> stReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPort);
        //保存标识
        String saveFlag = "0";
        //提交标识
        String subflag = "0";
        for (StReportPort stReportPort : stReportPortList) {
            if ("1".equals(stReportPort.getSsflag())) {
                saveFlag = "1";
                break;
            }
        }
        for (StReportPort stReportPort : stReportPortList) {
            if ("2".equals(stReportPort.getSsflag())) {
                subflag = "1";
                break;
            }
        }
        // 没有保存和提交标识 直接从服务报告进行查询
        if ("0".equals(saveFlag) && "0".equals(subflag)) {
            // 删除
            ArrayList<String> ids = new ArrayList();
            for (StReportPort stReportPort : stReportPortList) {
                ids.add(stReportPort.getId() + "");
            }
            if (ids.size() > 0) {
                String[] idlist = (String[]) ids.toArray(new String[ids.size()]);
                stReportPortMapper.deleteStReportPortByIds(idlist);
            }

            if ("idNull".equals(reportId)) {

                //重新插入
                if (datas.size() > 0) {
                    for (StReportPort stReportPort : datas) {
                        StReportPort newPort = new StReportPort();
                        newPort.setServiceNo(serviceNo);
                        newPort.setPortType("5");
                        newPort.setPartNumber(stReportPort.getPartNumber());
                        newPort.setLoanNumber(stReportPort.getLoanNumber());
                        newPort.setPartSerialNumber(stReportPort.getPartSerialNumber());
                        newPort.setPartName(stReportPort.getPartName());
                        newPort.setQtyCount(stReportPort.getQtyCount());
                        newPort.setPrice(stReportPort.getPrice());
                        newPort.setTotalprice(stReportPort.getTotalprice());
                        newPort.setSsflag("0");
                        newPort.setPartType(stReportPort.getPartType());
                        newPort.setRemark(stReportPort.getRemark());
                        newPort.setDamageSerialNumber(stReportPort.getDamageSerialNumber());
                        newPort.setDamagePic(stReportPort.getDamagePic());
                        newPort.setIsOutWarranty(stReportPort.getIsOutWarranty());
                        newPort.setStatusChange(stReportPort.getStatusChange());
                        newPort.setChangeRemark(stReportPort.getChangeRemark());
                        newPort.setPid(stReportPort.getPid());
                        newPort.setSyncFlag(stReportPort.getSyncFlag());
                        newPort.setSyncTime(stReportPort.getSyncTime());
                        newPort.setDelFlag(stReportPort.getDelFlag());
                        newPort.setCreateBy(stReportPort.getCreateBy());
                        newPort.setUpdateTime(new Date());
                        newPort.setAddress(stReportPort.getAddress());
                        newPort.setCreateTime(stReportPort.getCreateTime());
                        stReportPortMapper.insertStReportPort(newPort);
                    }
                    StReportPort searchStReportPortDel = new StReportPort();
                    searchStReportPortDel.setServiceNo(serviceNo);
                    searchStReportPortDel.setPortType("5");
                    searchStReportPortDel.setDelFlag("0");
                    searchStReportPortDel.setSsflag("0");
                    List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
                    stDatas = ReportPortList;
                }

            } else {
                ReportInfo reportInfo = stReportInfoMapper.selectStReportInfoById(new Long(reportId));
                //重新插入
                if (datas.size() > 0 && "0".equals(reportInfo.getSyncFlag())) {
                    for (StReportPort stReportPort : datas) {
                        StReportPort newPort = new StReportPort();
                        newPort.setServiceNo(serviceNo);
                        newPort.setPortType("5");
                        newPort.setPartNumber(stReportPort.getPartNumber());
                        newPort.setLoanNumber(stReportPort.getLoanNumber());
                        newPort.setPartSerialNumber(stReportPort.getPartSerialNumber());
                        newPort.setPartName(stReportPort.getPartName());
                        newPort.setQtyCount(stReportPort.getQtyCount());
                        newPort.setPrice(stReportPort.getPrice());
                        newPort.setTotalprice(stReportPort.getTotalprice());
                        newPort.setSsflag("0");
                        newPort.setPartType(stReportPort.getPartType());
                        newPort.setRemark(stReportPort.getRemark());
                        newPort.setDamageSerialNumber(stReportPort.getDamageSerialNumber());
                        newPort.setDamagePic(stReportPort.getDamagePic());
                        newPort.setIsOutWarranty(stReportPort.getIsOutWarranty());
                        newPort.setStatusChange(stReportPort.getStatusChange());
                        newPort.setChangeRemark(stReportPort.getChangeRemark());
                        newPort.setPid(stReportPort.getPid());
                        newPort.setSyncFlag(stReportPort.getSyncFlag());
                        newPort.setSyncTime(stReportPort.getSyncTime());
                        newPort.setDelFlag(stReportPort.getDelFlag());
                        newPort.setCreateBy(stReportPort.getCreateBy());
                        newPort.setUpdateTime(new Date());
                        newPort.setAddress(stReportPort.getAddress());
                        newPort.setCreateTime(stReportPort.getCreateTime());
                        stReportPortMapper.insertStReportPort(newPort);
                    }
                    StReportPort searchStReportPortDel = new StReportPort();
                    searchStReportPortDel.setServiceNo(serviceNo);
                    searchStReportPortDel.setPortType("5");
                    searchStReportPortDel.setDelFlag("0");
                    searchStReportPortDel.setSsflag("0");
                    List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
                    stDatas = ReportPortList;
                }
            }


        }


        // 有保存的
        if ("1".equals(saveFlag)) {
            StReportPort searchStReportPortDel = new StReportPort();
            searchStReportPortDel.setServiceNo(serviceNo);
            searchStReportPortDel.setPortType("5");
            searchStReportPortDel.setDelFlag("0");
            searchStReportPortDel.setSsflag("1");
            List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
            stDatas = ReportPortList;
        }
        // 有保存的
        if ("1".equals(subflag)) {
            StReportPort searchStReportPortDel = new StReportPort();
            searchStReportPortDel.setServiceNo(serviceNo);
            searchStReportPortDel.setPortType("5");
            searchStReportPortDel.setDelFlag("0");
            searchStReportPortDel.setSsflag("2");
            List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
            stDatas = ReportPortList;
        }


//        if (stReportPortList.size() > 0) {
//            stDatas = stReportPortList;
//        } else {
//            StReportPort searchStReportPortDel = new StReportPort();
//            searchStReportPortDel.setServiceNo(serviceNo);
//            searchStReportPortDel.setPortType("5");
//            searchStReportPortDel.setDelFlag("1");
//            List<StReportPort> stReportPortListDel = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
//            if (stReportPortListDel.size() > 0) {
//                stDatas = null;
//                return new ResponseWithData<>().success(stDatas).code(200);
//            } else {
//
//            }
//        }


        return new ResponseWithData<>().success(stDatas).code(200);
    }

    /**
     * 查看删除后的接口
     * 已完成报告【服务报告或电话解决报告】中的备件信息。
     *
     * @param serviceNo
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/9 2:53 下午
     */
    @Override
    public ResponseWithData<?> selctcInstallationQualityReportPortByDel(String serviceNo, String helpId, String type) {
        // 查询
        List<StReportPort> list = new ArrayList<StReportPort>();
        StReportPort searchStReportPort = new StReportPort();

        searchStReportPort.setServiceNo(serviceNo);
        searchStReportPort.setPortType("5");
        searchStReportPort.setDelFlag("0");
        List<StReportPort> stReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPort);
        //保存标识
        String saveFlag = "0";
        //提交标识
        String subflag = "0";
        for (StReportPort stReportPort : stReportPortList) {
            if ("1".equals(stReportPort.getSsflag())) {
                saveFlag = "1";
                break;
            }
        }
        for (StReportPort stReportPort : stReportPortList) {
            if ("2".equals(stReportPort.getSsflag())) {
                subflag = "1";
                break;
            }
        }
        // 没有保存和提交标识 直接从服务报告进行查询
        if ("0".equals(saveFlag) && "0".equals(subflag)) {
            StReportPort searchStReportPortDel = new StReportPort();
            searchStReportPortDel.setServiceNo(serviceNo);
            searchStReportPortDel.setPortType("5");
            searchStReportPortDel.setDelFlag("0");
            searchStReportPortDel.setSsflag("0");
            List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
            list = ReportPortList;
        }


        // 有保存的
        if ("1".equals(saveFlag)) {
            StReportPort searchStReportPortDel = new StReportPort();
            searchStReportPortDel.setServiceNo(serviceNo);
            searchStReportPortDel.setPortType("5");
            searchStReportPortDel.setDelFlag("0");
            searchStReportPortDel.setSsflag("1");
            List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
            list = ReportPortList;
        }
        // 有提交的
        if ("1".equals(subflag)) {
            StReportPort searchStReportPortDel = new StReportPort();
            searchStReportPortDel.setServiceNo(serviceNo);
            searchStReportPortDel.setPortType("5");
            searchStReportPortDel.setDelFlag("0");
            searchStReportPortDel.setSsflag("2");
            List<StReportPort> ReportPortList = stReportPortMapper.selectStReportPortList(searchStReportPortDel);
            list = ReportPortList;
        }


        return new ResponseWithData<>().success(list).msg("查询成功").code(200);
    }


    /**
     * 添加服务报告备件信息
     *
     * @param reportPort
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020-03-03 14:00
     */
    @Override
    public ResponseWithData<?> addFieldServiceReportPort(StReportPort reportPort) {
        // 参数校验 start
        if (reportPort.getEnginnerId() == null) {
            return new ResponseWithData<>().error("enginnerId cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getServiceNo())) {
            return new ResponseWithData<>().error("serviceNo cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getPid())) {
            return new ResponseWithData<>().error("pid cannot be empty！");
        }
        if (null == reportPort.getQtyCount()) {
            return new ResponseWithData<>().error("qtyCount cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getPartNumber())) {
            return new ResponseWithData<>().error("partNumber cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getHelpId())) {
            return new ResponseWithData<>().error("helpId cannot be empty！");
        }
        if (StringUtils.isEmpty(reportPort.getAnnexHelpId())) {
            return new ResponseWithData<>().error("annexHelpId cannot be empty！");
        }
        // 参数校验 end
        // 数据入库 strat
        reportPort.setCreateTime(new Date());
        reportPort.setDelFlag("0");
        reportPort.setSyncFlag("0");
        stReportPortMapper.insertStReportPort(reportPort);

        if (StringUtils.isNotEmpty(reportPort.getHelpId())) {
            Annex annex = new Annex();
            annex.setUpdateTime(new Date());
            annex.setRelationId(reportPort.getId().toString());
            annex.setTblName("st_report_port");
            annex.setHelpId(reportPort.getHelpId());
            annex.setServiceNo(reportPort.getServiceNo());
            annexMapper.updateAnnexByHelpIdAndServiceNo(annex);
        }

        // 更新现场服务报告备件总费用及总费用
        List<StReportPort> stReportPorts = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(reportPort.getServiceNo(), reportPort.getHelpId(), "1");
        if (null != stReportPorts && stReportPorts.size() > 0) {
            Double partsTotallFee = 0.0;
            for (StReportPort stReportPort : stReportPorts) {
                partsTotallFee += stReportPort.getTotalprice();
            }
            StServiceReport serviceReport = serviceReportMapper.selectStServiceReportByServiceNoAndHelpId(reportPort.getServiceNo(), reportPort.getHelpId());
            if (null != serviceReport) {
                Double partsTotallFeeTmp = serviceReport.getPartsTotallFee();
                Double totalBilled = serviceReport.getTotalBilled();
                if (null != partsTotallFeeTmp) {
                    serviceReport.setPartsTotallFee(partsTotallFeeTmp);
                    if (null != totalBilled) {
                        Double newTotalBilled = partsTotallFee;
                        serviceReport.setTotalBilled(newTotalBilled);
                    }
                    serviceReportMapper.updateStServiceReportByHelpIdAndServiceNo(serviceReport);
                }
            }
        }

        // 数据入库 end
        return new ResponseWithData<>().success(reportPort).code(200);
    }

    /**
     * 移除服务报告备件信息
     *
     * @param enginnerId   工程师ID
     * @param reportPortId 备件表主键ID
     * @return com.ruoyi.system.api.ResponseWithData
     * @author yzg
     * @date 2020-03-03 15:02
     */

    @Override
    public ResponseWithData removeFieldServiceReportPort(Long enginnerId, Long reportPortId) {
        StReportPort reportPort = stReportPortMapper.selectStReportPortById(reportPortId);
        if (null != reportPort) {
            reportPort.setUpdateBy(enginnerId.toString());
            reportPort.setUpdateTime(new Date());
            annexMapper.deleteAnnexByRelationId(reportPortId.toString());
            stReportPortMapper.updateStReportPortById(reportPort);

            // 更新现场服务报告备件总费用及总费用
            List<StReportPort> stReportPorts = stReportPortMapper.selectStReportPortListByServiceNoAndHelpIdAndPortType(reportPort.getServiceNo(), reportPort.getHelpId(), "1");
            if (null != stReportPorts && stReportPorts.size() > 0) {
                Double partsTotallFee = 0.0;
                for (StReportPort stReportPort : stReportPorts) {
                    partsTotallFee += stReportPort.getTotalprice();
                }
                StServiceReport serviceReport = serviceReportMapper.selectStServiceReportByServiceNoAndHelpId(reportPort.getServiceNo(), reportPort.getHelpId());
                if (null != serviceReport) {
                    Double partsTotallFeeTmp = serviceReport.getPartsTotallFee();
                    Double totalBilled = serviceReport.getTotalBilled();
                    if (null != partsTotallFeeTmp) {
                        serviceReport.setPartsTotallFee(partsTotallFeeTmp);
                        if (null != totalBilled) {
                            Double newTotalBilled = partsTotallFee;
                            serviceReport.setTotalBilled(newTotalBilled);
                        }
                        serviceReportMapper.updateStServiceReportByHelpIdAndServiceNo(serviceReport);
                    }
                }
            }


            return new ResponseWithData<>().success("移除备件信息成功").code(200);
        } else {
            return new ResponseWithData<>().success("备件信息不存在").code(201);
        }

    }

    @Override
    public ResponseWithData selectPartQty(String partNumber, String serviceNo, String helpId, String enginnerId) throws Exception {
        int x = stReportPortMapper.selectUnsubmittedPartQtyCount(partNumber, enginnerId);
        int y = stReportPortMapper.selectUnsubmittedPartQtyCountByServiceNoAndHelpId(partNumber, serviceNo, helpId, enginnerId);
        return new ResponseWithData().success((x + y)).code(200);
    }

    @Override
    public ResponseWithData selectPriceByPartcode(String partCode) throws Exception {
        JSONObject jsonObject = ccService.queryPriceByPartCode(partCode);
        return new ResponseWithData().success(jsonObject).code(200).msg("查询成功");
    }
}
